﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Deixei.Infrastructure
{
    public class PagedList<T>
    {
        public PagedList(IQueryable<T> source, int index, int pageSize)
        {
            ContentList = new List<T>();

            index = index == 0 ? 1 : index;
            pageSize = pageSize == 0 ? 10 : pageSize;

            this.TotalCount = source.Count();
            this.PageSize = pageSize;
            this.PageIndex = index;
            ContentList.AddRange(source.Skip(( index - 1 ) * pageSize).Take(pageSize).ToList());
            this.TotalPages = CalculatePageCount(TotalCount, pageSize, isZeroIndex: false);

            MessageResult = new MessageResult("PagedList", CommonResources.Success, MessageStatus.Success);
        }
        public PagedList(List<T> source, int index, int pageSize, int totalRecords)
        {
            
            ContentList = new List<T>();

            index = index == 0 ? 1 : index;
            pageSize = pageSize == 0 ? 10 : pageSize;

            this.TotalCount = totalRecords;

            this.PageSize = pageSize;
            this.PageIndex = index;
            ContentList.AddRange(source.Skip(( index - 1 ) * pageSize).Take(pageSize).ToList());
            this.TotalPages = CalculatePageCount(totalRecords, pageSize, isZeroIndex: false);
            MessageResult = new MessageResult("PagedList", CommonResources.Success, MessageStatus.Success);
        }

        private int CalculatePageCount(int recordNumber, int pageSize, bool isZeroIndex = false)
        {
            ////>If your first record is numbered zero (0), then 
            if ( isZeroIndex )
            { 
                return 1 + (int)(recordNumber / pageSize);
            }
            ////>If your first record is numbered one (1), then 
            return 1 + (int)( ( recordNumber - 1 ) / pageSize );
        }

        public List<T> ContentList { get; set; }

        public MessageResult MessageResult { get; set; }

        public int TotalPages
        {
            get;
            set;
        }

        public int TotalCount
        {
            get;
            set;
        }

        public int PageIndex
        {
            get;
            set;
        }

        public int PageSize
        {
            get;
            set;
        }

        public bool HasPreviousPage
        {
            get
            {
                return ( PageIndex > 1 );
            }
        }

        public bool HasNextPage
        {
            get
            {
                return ( PageIndex * PageSize ) <= TotalCount;
            }
        }



        public int StartPage { get { return 1; }}
        public int EndPage { get { return TotalPages; } }

        public int NextPage { 
            get {
                if ( HasNextPage )
                {
                    return PageIndex + 1;
                }
                else
                {
                    return EndPage;
                }
            }
        }
        public int PreviousPage { 
            get {

                if ( HasPreviousPage )
                {
                    return PageIndex - 1;                
                }
                else
                {
                    return StartPage;
                }

            } 
        }

    }

}
